/*******************************************************************************
* This file declares all the variables in flash to store the configuration of
* the system and also provides functions to access them.
*
* Author: Kong Wai Weng @ Cytron Technologies Sdn. Bhd.
*******************************************************************************/



#ifndef _FLASH_H
#define _FLASH_H



#include <libpic30.h>
#include <string.h>
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "variables.h"
#include "hal_uart.h"
#include "system.h"



/*******************************************************************************
* STRUCTURE, UNION AND ENUMERATION DEFINITION                                  *
*******************************************************************************/

// System configuration.
struct SYSTEM_CONFIG {
	// Sensors calibration data.
	signed int siGyroXMid;		// Gyro X value for 0 rad/s.
	float fGyroXScale;			// Scale to change gyro value to rad/s.
	
	signed int siGyroYMid;		// Gyro Y value for 0 rad/s.
	float fGyroYScale;			// Scale to change gyro value to rad/s.
	
	signed int siGyroZMid;		// Gyro Z value for 0 rad/s.
	float fGyroZScale;			// Scale to change gyro value to rad/s.
	
	
	
	signed int siAccXMid;		// Acc X value for 0G.
	float fAccXScale;			// Scale to change the value to G.
	
	signed int siAccYMid;		// Acc Y value for 0G.
	float fAccYScale;			// Scale to change the value to G.
	
	signed int siAccZMid;		// Acc Z value for 0G.
	float fAccZScale;			// Scale to change the value to G.
	
	
	
	signed int siMagXMid;		// Mag X value for 0 gauss.
	float fMagXScale;			// Scale to change the value to +-1 for Earth magnetic field.
	
	signed int siMagYMid;		// Mag Y value for 0 gauss.
	float fMagYScale;			// Scale to change the value to +-1 for Earth magnetic field.
	
	signed int siMagZMid;		// Mag Z value for 0 gauss.
	float fMagZScale;			// Scale to change the value to +-1 for Earth magnetic field.
	
	
	
//	// RC Rx calibration data.
//	signed int siRcRxRollMid;	// Mid value for RC Rx roll channel.
//	float fRcRxRollScale;		// Scale to change the value to +-1.
//	
//	signed int siRcRxPitchMid;	// Mid value for RC Rx pitch channel.
//	float fRcRxPitchScale;		// Scale to change the value to +-1.
//	
//	signed int siRcRxYawMid;	// Mid value for RC Rx yaw channel.
//	float fRcRxYawScale;		// Scale to change the value to +-1.
//	
//	signed int siRcRxThrMin;	// Minimum value for RC Rx throttle channel.
//	float fRcRxThrScale;		// Scale to change the value to 0 - 1.
	
	
	
	// The gain for the control system.
	float fRollKp;
	float fRollKd;
	float fRollKi;
	
	float fPitchKp;
	float fPitchKd;
	float fPitchKi;
	
	float fNavKp;
	float fNavKi;
	
	float fCrossTrackKp;
	
	float fAirspeedKp;
	float fAirspeedKi;
	
	// Aileron - Rudder mix ratio.
	float fAilRudRatio;
};



/*******************************************************************************
* PUBLIC FUNCTION PROTOTYPES                                                   *
*******************************************************************************/

/*******************************************************************************
* PUBLIC FUNCTION: vLoadFlashData
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Read all the value from flash and save them in xSystemConfig.
*
*******************************************************************************/
void vLoadFlashData(void);



/*******************************************************************************
* PUBLIC FUNCTION: vSaveFlashData
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Save all the value from xSystemConfig into the flash.
*
*******************************************************************************/
void vSaveFlashData(void);



/*******************************************************************************
* PUBLIC FUNCTION: vPrintFlashData
*
* PARAMETERS:
* ~ void
*
* RETURN:
* ~ void
*
* DESCRIPTIONS:
* Print out the value for each member of system confir.
*
*******************************************************************************/
void vPrintFlashData(void);



#endif
